<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/2/23
 * Time: 17:19
 */
class Model_Client extends PhalApi_Model_NotORM{
    protected function getTableName($id){
        return 'p_shop';
    }
    /**
     *活动商家列表
     */
    public function getClientList(){
        //return $this->getORM()->select('*')->fetchAll();
        return $this->getORM()->select('shopid,fvotes as votes,cimg')->fetchAll();
    }
    /*
     * 更新所有数据 到假数据
     */
    public function updateAllinfo(){
        $sql  = "select votes,shopid from p_shop";
        $data = $this->getORM()->queryAll($sql, array());
        foreach($data as $k => $v){

                $sql   = "update p_shop set fvotes=? where shopid=?";
                $array = array($v['votes'], $v['shopid']);
                $this->getORM()->queryAll($sql, $array);

        }
    }
    /*
     *商家投票假数据  *1.2
     */
    public function getFalseVote2(){

        $sql = "select votes,shopid from p_shop";
        $data = $this->getORM()->queryAll($sql,array());
        foreach($data as $k=>$v){

            //排除商家id
            $shopid = array(3,29,30,31);
            $sid = $v['shopid'];
            if(in_array($sid,$shopid)){
                $sql = "update p_shop set fvotes=? where shopid=?";
                $array = array($v['votes'],$sid);
                $this->getORM()->queryAll($sql,$array);
            }else{
                $fvotes = ceil($v['votes']*1.2);
                $sql = "update p_shop set fvotes=? where shopid=?";
                $array = array($fvotes,$sid);
                $this->getORM()->queryAll($sql,$array);
            }
        }

       //查询a
       return $this->getORM()->select('shopid,fvotes as votes,cimg')->fetchAll();
    }
    /*
      *商家投票假数据    *1.3
     */
    public function getFalseVote3(){
        $sql = "select votes,shopid from p_shop";
        $data = $this->getORM()->queryAll($sql,array());
        foreach($data as $k=>$v){

            //排除商家id
            $shopid = array(3,29,30,31);
            $sid = $v['shopid'];
            if(in_array($sid,$shopid)){
                $sql = "update p_shop set fvotes=? where shopid=?";
                $array = array($v['votes'],$sid);
                $this->getORM()->queryAll($sql,$array);
            }else{
                $fvotes = ceil($v['votes']*1.3);
                $sql = "update p_shop set fvotes=? where shopid=?";
                $array = array($fvotes,$sid);
                $this->getORM()->queryAll($sql,$array);
            }
        }

        //查询a
         return $this->getORM()->select('shopid,fvotes as votes,cimg')->fetchAll();
    }
    /*
     * 商家票数加1
     */
    public function clientVote($obj){
        $shopid = $obj->shopid;
        $shopid=explode(',',$shopid);
       return $this->getORM()->where('shopid',$shopid)->update(array('votes'=>new NotORM_Literal("votes + 1"),'fvotes'=>new NotORM_Literal("fvotes + 1")));
    }
}
